﻿using RM.Weighing.BLL;
using RM.Weighing.Models;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace RM.Weighing.Page
{
    public partial class UIProduct : UIPage
    {
        private List<Product> products;
        private ProductBLL productBLL = new ProductBLL();
        public UIProduct()
        {
            InitializeComponent();
        }


        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            if (uiTextBox1.Text == string.Empty)
            {
                ShowErrorTip("请输入产品名称！");
                return;
            }
            if (uiTextBox2.Text == string.Empty)
            {
                ShowErrorTip("请输入产品代码！");
                return;
            }
            Product product = new Product()
            {
                ProductName = uiTextBox1.Text.Trim(),
                ProductCode = uiTextBox2.Text.Trim(),
                WeightLow = uiIntegerUpDown2.Value.ToString().Trim(),
                WeightLimit = uiIntegerUpDown1.Value.ToString().Trim(),
            };
            bool v = productBLL.Exists(product);
            if (v)
            {
                productBLL.Update(product);
            }
            else
            {
                productBLL.Add(product);
            }
            uiTextBox1.Text = "";
            uiTextBox2.Text = "";
            uiIntegerUpDown1.Value = 1300;
            uiIntegerUpDown2.Value = 1300;
            UpdateData();
        }

        private void UIProduct_Initialize(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void UpdateData()
        {
            products = productBLL.GetAllModel();
            if (products != null || products.Count > 0)
            {
                for (int i = 0; i < products.Count; i++)
                {
                    products[i].XH = i + 1;
                }
            }
            uiDataGridView1.DataSource = products;
        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = ((DataGridView)sender).SelectedCells[0].RowIndex;
            uiTextBox1.Text = products[a].ProductName;
            uiTextBox2.Text = products[a].ProductCode;
            uiIntegerUpDown2.Value = Convert.ToInt32(products[a].WeightLow);
            uiIntegerUpDown1.Value = Convert.ToInt32(products[a].WeightLimit);
        }

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            if (uiTextBox1.Text == string.Empty)
            {
                ShowErrorTip("请输入产品名称！");
                return;
            }
            if (uiTextBox2.Text == string.Empty)
            {
                ShowErrorTip("请输入产品代码！");
                return;
            }
            Product product = new Product()
            {
                ProductName = uiTextBox1.Text,
                ProductCode = uiTextBox2.Text,
            };
            //创建存储参数的数组
            SqlParameter[] sqlParameters = new[]
            {
                //将产品名称存入
                new SqlParameter("@productName",SqlDbType.NVarChar,100){Value = product.ProductName ?? string.Empty},
                //将产品条码存入
                new SqlParameter("@productCode",SqlDbType.NVarChar,100){Value = product.ProductCode ?? string.Empty},
            };
            productBLL.Delete("ProductName = @ProductName AND ProductCode=@ProductCode;", sqlParameters);
            uiTextBox1.Text = "";
            uiTextBox2.Text = "";
            uiIntegerUpDown1.Value = 1300;
            uiIntegerUpDown2.Value = 1300;
            UpdateData();
        }
    }
}
